#include <QPainter>
#include <QBoxLayout>
#include <QStackedWidget>
#include <QStackedLayout>

#include "toolbutton.h"

#include "dockstack.h"

#define DockTitleHeight (27)

DockStack::DockStack(SideButtonPanel::Direction direction,
                     QWidget *parent) : QWidget(parent),
    m_sidePanel(new SideButtonPanel(direction)),
    m_minimize(new ToolButton(this)),
    m_titleStack(new QStackedWidget(this))
{
    setContentsMargins(0, 0, 0, 0);
    //Add widget.
    QBoxLayout *mainLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
    switch(direction)
    {
    case SideButtonPanel::North:
        mainLayout->setContentsMargins(0, 1, 0, 0);
        break;
    case SideButtonPanel::West:
        mainLayout->setContentsMargins(0, 0, 1, 0);
        break;
    case SideButtonPanel::East:
        mainLayout->setContentsMargins(1, 0, 0, 0);
        break;
    }
    mainLayout->setSpacing(0);
    setLayout(mainLayout);
    //Add layout data.
    QBoxLayout *titleLayout = new QBoxLayout(QBoxLayout::LeftToRight);
    titleLayout->addWidget(m_titleStack, 1);
    titleLayout->addWidget(m_minimize);
    mainLayout->addLayout(titleLayout);
    mainLayout->addSpacing(1);
    //Add caption label and buttons.
    m_titleStack->setFixedHeight(DockTitleHeight);
    m_minimize->setIcon(QIcon(":/actions/minimize.png"));
    //Initial the layout.
    m_dockStack = new QStackedLayout();
    mainLayout->addLayout(m_dockStack, 1);
}

void DockStack::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(QColor(209, 209, 209));
    //Draw the line to draw the line.
    switch(m_sidePanel->direction())
    {
    case SideButtonPanel::North:
        // Draw the top line.
        painter.drawLine(0, 0, width(), 0);
        break;
    case SideButtonPanel::West:
        painter.drawLine(width(), 0, width() - 1, height());
        break;
    case SideButtonPanel::East:
        painter.drawLine(0, 0, 0, height());
        break;
    }
    //Draw the title line.
    painter.drawLine(0, DockTitleHeight, width(), DockTitleHeight);
}

SideButtonPanel *DockStack::sidePanel() const
{
    return m_sidePanel;
}
